package com.service.abolishing.rabbitmq;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rabbitmq.client.Channel;
import com.service.abolishing.config.RabbitConfig;
import com.service.abolishing.dto.DeviceTerminalUser;
import com.service.abolishing.entity.CloudCampusInfo;
import com.service.abolishing.task.CloudCampusApi;
import com.service.abolishing.task.ScheduledService;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @ClassName: MsgReceiver
 * @Description:
 * @Author guochuang
 * @Date 2021/10/25
 * @Version 1.0
 */
@Component
public class MsgReceiver {
    private final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
    private final ExecutorService product = new ThreadPoolExecutor(10,20,200L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>(),namedThreadFactory);
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    RedisUtil redisUtil;
    @Resource
    private CloudCampusApi cloudCampusApi;
    @Resource
    private ScheduledService scheduledService;
    @RabbitListener(queues = RabbitConfig.QUEUE_C)
    public void processC(Channel channel, String content, Message message) throws Exception {
        DeviceTerminalUser deviceTerminalUser = JsonXMLUtils.json2obj(content, DeviceTerminalUser.class);
        logger.info(">>>>>>>>>>>>>>>>>message：" + deviceTerminalUser.getCloudCampusInfo().getDomain());
        String token = "";
        HttpHeaders token1 = deviceTerminalUser.getToken();
        try {
            CloudCampusInfo cloudCampusInfo = deviceTerminalUser.getCloudCampusInfo();
            if (redisUtil.exist(deviceTerminalUser.getCloudCampusInfo().getTenantName().concat("token"))) {
                token = redisUtil.getValue(deviceTerminalUser.getCloudCampusInfo().getTenantName().concat("token"));
            } else {
                //获取token
                token = cloudCampusApi.getToken(cloudCampusInfo.getTenantName(), cloudCampusInfo.getTenantPwd(), deviceTerminalUser.getToken(), cloudCampusInfo.getDomain());
                redisUtil.set(cloudCampusInfo.getTenantName().concat("token"), 1, token, 10);
            }
            //添加token
            token1.add("X-AUTH-TOKEN", token);
            product.submit(() -> {
                cloudCampusApi.getByDeviceTerminalUserList("online",deviceTerminalUser.getApDetails(),token1,cloudCampusInfo.getDomain(),deviceTerminalUser.getTimeStamp());
            });
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>error");
        }
    }
    @RabbitListener(queues = RabbitConfig.QUEUE_A)
    public void processA(Channel channel, String content, Message message) throws Exception {
        DeviceTerminalUser deviceTerminalUser = JsonXMLUtils.json2obj(content, DeviceTerminalUser.class);
        logger.info(">>>>>>>>>>>>>>>>>message：" + JsonXMLUtils.obj2json(content));
    }
}
